package com.lemon.stock.compose

import android.view.ContextThemeWrapper
import android.widget.CalendarView
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.AlertDialog
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.compose.ui.window.Dialog
import java.util.*


@Composable
fun showDialog(){
    AlertDialog(
        text = {Text("MEssAge")},
        buttons = {
            TextButton(
                onClick = {  },
                modifier = Modifier.padding(8.dp)
            ) {
                Text(text = "Ok")
            }
        },
        onDismissRequest = {  }
    )
}



/**
 * 废弃不用，留着参考学习
 */
@Composable
fun DatePickerCustom(onDismissRequest: ()-> Unit, onDateSelected: (Calendar)->Unit) {
    val theme = LocalContext.current.theme
    val calendar = remember {mutableStateOf(Calendar.getInstance()) }
    Dialog(onDismissRequest = { onDismissRequest() }) {
        Column(
            modifier = Modifier
                .wrapContentSize()
                .background(
                    color = MaterialTheme.colors.surface,
                    shape = RoundedCornerShape(size = 16.dp)
                )
        ) {
            AndroidView(
                modifier = Modifier.wrapContentSize(),
                factory = {context ->
                    CalendarView(ContextThemeWrapper(context, theme))
                },
                update = { view->
                    view.setOnDateChangeListener{view, year, month, dayOfMonth->
                        calendar.value.set(Calendar.YEAR, year)
                        calendar.value.set(Calendar.MONTH, month)
                        calendar.value.set(Calendar.DAY_OF_MONTH, dayOfMonth)
                    }
                }
            )
            Row(
                modifier = Modifier
                    .align(Alignment.End)
                    .padding(bottom = 16.dp, end = 16.dp)
            ){
                TextButton(onClick = onDismissRequest) {
                    Text(text = "Cancel")
                }
                TextButton(onClick = {
                    onDateSelected(calendar.value)
                    onDismissRequest()
                }) {
                    Text(text = "Ok")
                }
            }
        }
    }
}